<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-page-header @back="back" :content="content">
            </el-page-header>
            <el-form ref="form" class="edit-el-form"  :model="form" :rules="rules" label-width="auto">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" required></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model.number="form.sort" required>
                      <template slot="append">注：排序越小越靠前，默认100</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="图片" prop="url">
                    <Upload :limit="1" :file_list="banner_list" :catch_urls="catch_urls" :remove_urls="remove_urls" :upload_tip="upload_tip"></Upload>
                </el-form-item>
                <el-form-item label="跳转位置" prop="link_type">
                   <el-radio-group v-model="form.link_type" size='small' border>
                         <el-radio-button  label="1">内部跳转</el-radio-button>
                         <el-radio-button  label="2">外部跳转</el-radio-button>
                   </el-radio-group>
                </el-form-item>
                <el-form-item label="链接地址" prop="link_src">
                    <el-input v-model="form.link_src" required></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.enabled">
                        <el-radio border label="0">禁用</el-radio>
                        <el-radio border label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item v-if="edit_type!=0">
                    <el-button type="primary" @click="submitForm('form')" :loading="loading">{{button_name}}</el-button>
                    <el-button @click="back">取消</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script>
    import { getBannerInfo,editBanner } from '@/api/banner.js'
    import Upload from '@/components/Upload.vue';
    export default {
        name:'BannerEdit',
        components:{Upload},
        data() {
            return {
                form: {
                    name: '',
                    sort: 100,
                    url:'',
                    link_type:'1',
                    link_src:'',
                    enabled: '1',
                },
                //校验规则
                rules:{
                    name: [
                        { required:true , message:'请输入名称',trigger:'blur' }
                    ],
                    sort:[
                        { required:true , message:'请输入排序号',trigger:'blur' },
                        { type:'number' , message:'排序号必须为数字' }
                    ],
                    url:[
                        { required:true , message:'请上传图片'}
                    ]
                },
                //加载组件
                loading: false,
                button_name:'立即提交',
                content:'横幅添加',
                child_content:"",
                banner_list:[],
                upload_tip:'只能上传jpg/png/gif文件，且不超过500kb，仅允许上传一张',
                allbannertype:this.$store.getters.getAllBannerType
            }
        },
        methods: {
            submitForm(formName){
                    let that = this;
                    this.$refs[formName].validate((valid) => {
                            if (valid) {
                                this.loading=true;
                                this.button_name='正在提交';
                                var send_data={
                                    name:this.form.name,
                                    sort:this.form.sort,
                                    url:this.form.url,
                                    link_type:this.form.link_type,
                                    link_src:this.form.link_src,
                                    enabled:this.form.enabled,
                                };
                                if(this.id!=-1){
                                    send_data.id=this.id;
                                }
                                editBanner(send_data).then(function(response){
                                    let code=response.data.code;
                                    let type=code==1?'success':'error';
                                    that.$message({
                                        message:response.data.msg,
                                        type:type,
                                        onClose:function(e){
                                            that.loading=false;
                                            if(code==1){
                                               that.$router.push('/system/bannerlist');
                                            }else{
                                               that.button_name='立即提交';
                                            }
                                        }
                                    });
                                })
                            }else{
                                setTimeout(()=>{
                                  const is_error=document.getElementsByClassName('is-error');
                                  is_error[0].querySelector('input,button').focus();
                                },100);
                                return false;
                            }
                    })
            },
            back(){
                this.$router.go(-1);
            },
            //富文本内容
            catchData(e){
               this.form.content=e;
            },
            //这边只有一张，如果有数组就移除数组中的下标
            remove_urls(remove_index){
              if(remove_index==0){
                 this.form.url=''
              }
            },
            //获取组件返回的图片路径
            catch_urls(urls){
               this.form.url=urls[0];
            }
        },
        created(){
            if(this.id!=-1){
                var send_data={
                    id:this.id
                }
                let that=this;
                getBannerInfo(send_data).then(function(response){
                   if(response.data.code==1){
                      that.form.name=response.data.info.name;
                      that.form.sort=response.data.info.sort;
                      that.form.url=response.data.info.url;
                      that.form.link_type=response.data.info.link_type.toString();
                      that.form.link_src=response.data.info.link_src;
                      that.form.enabled=response.data.info.enabled.toString();
                      if(response.data.info.url.length>0){
                        that.banner_list=[{
                            name:0,
                            url:response.data.info.url
                        }];
                      }
                   }
                })
            }
            //修改标题
            if(this.edit_type==1){
                this.content='横幅添加';
            }else if(this.edit_type==0){
                this.content='横幅详情';
            }else if(this.edit_type==2){
                this.content='横幅编辑';
            }
        },
        computed:{
            id(){
                return this.$route.params.id;
            },
            //是否可编辑
            edit_type(){
                return this.$route.params.edit_type;
            }
        }
    }
</script>

<style>
    .edit-el-form{
        margin-top:30px;
    }
</style>
